package com.xdb.sqlparser.dialect.oracle.ast.statement;

import com.xdb.sqlparser.ast.SQLExpr;
import com.xdb.sqlparser.ast.statement.SQLJoinTableSource;
import com.xdb.sqlparser.dialect.oracle.ast.clause.OracleFlashbackQueryClause;
import com.xdb.sqlparser.dialect.oracle.visitor.OracleASTVisitor;
import com.xdb.sqlparser.util.SQLUtils;
import com.xdb.sqlparser.visitor.SQLAstVisitor;

public class OracleSelectJoin extends SQLJoinTableSource implements OracleSelectTableSource {

	protected OracleSelectPivotBase pivot;
	protected OracleFlashbackQueryClause flashback;

	public OracleSelectJoin(String alias) {
		super(alias);
	}

	public OracleSelectJoin() {

	}

	public OracleFlashbackQueryClause getFlashback() {
		return flashback;
	}

	public void setFlashback(OracleFlashbackQueryClause flashback) {
		this.flashback = flashback;
	}

	public OracleSelectPivotBase getPivot() {
		return pivot;
	}

	public void setPivot(OracleSelectPivotBase pivot) {
		this.pivot = pivot;
	}

	@Override
	protected void accept0(SQLAstVisitor visitor) {
		this.accept0((OracleASTVisitor) visitor);
	}

	protected void accept0(OracleASTVisitor visitor) {
		if (visitor.visit(this)) {
			acceptChild(visitor, this.left);
			acceptChild(visitor, this.right);
			acceptChild(visitor, this.condition);
			acceptChild(visitor, this.using);
			acceptChild(visitor, this.flashback);
		}

		visitor.endVisit(this);
	}

	public void output(StringBuffer buf) {
		this.left.output(buf);
		buf.append(JoinType.toString(this.joinType));
		this.right.output(buf);

		if (this.condition != null) {
			buf.append(" ON ");
			this.condition.output(buf);
		}

		if (this.using.size() > 0) {
			buf.append(" USING (");
			int i = 0;
			for (int size = this.using.size(); i < size; ++i) {
				if (i != 0) {
					buf.append(", ");
				}
				((SQLExpr) this.using.get(i)).output(buf);
			}
			buf.append(")");
		}
	}

	public String toString() {
		return SQLUtils.toOracleString(this);
	}
}
